package com.lk.base.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import com.alibaba.druid.util.StringUtils;

import redis.clients.jedis.JedisPoolConfig;

@Repository
@Configuration
public class ApplicationRedisConfig{
	
	//服务地址
	@Value("${spring.redis.host}")
	public String host;
	//数据库
	@Value("${spring.redis.database}")
	public int database;
	//端口
	@Value("${spring.redis.port}")
	public int port;
	//密码
	@Value("${spring.redis.password}")
	public String password;
	@Value("${spring.redis.timeout}")
	private int timeout;
	//最大连接数
	@Value("${spring.redis.jedis.pool.max-active}")
	public int maxActive;
	//最大等待时间
	@Value("${spring.redis.jedis.pool.max-wait}")
	public int maxWait;
	//最大空闲
	@Value("${spring.redis.jedis.pool.max-idle}")
	public int maxIdle;
	//最小空闲
	@Value("${spring.redis.jedis.pool.min-idle}")
	public int minIdle;
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getMaxActive() {
		return maxActive;
	}
	public void setMaxActive(int maxActive) {
		this.maxActive = maxActive;
	}
	public int getMaxWait() {
		return maxWait;
	}
	public void setMaxWait(int maxWait) {
		this.maxWait = maxWait;
	}
	public int getMaxIdle() {
		return maxIdle;
	}
	public void setMaxIdle(int maxIdle) {
		this.maxIdle = maxIdle;
	}
	public int getMinIdle() {
		return minIdle;
	}
	public void setMinIdle(int minIdle) {
		this.minIdle = minIdle;
	}
	public int getTimeout() {
		return timeout;
	}
	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}
	public int getDatabase() {
		return database;
	}
	public void setDatabase(int database) {
		this.database = database;
	}
	
	
	@Bean(name = "myRedisTemplate")
	public StringRedisTemplate redisTemplate() {
        StringRedisTemplate temple = new StringRedisTemplate();
        temple.setConnectionFactory(connectionFactory());
        return temple;
    }

    public RedisConnectionFactory connectionFactory() {
        JedisConnectionFactory jedis = new JedisConnectionFactory();
        jedis.setHostName(host);
        jedis.setPort(port);
        jedis.setTimeout(timeout);
        if (!StringUtils.isEmpty(password)) {
            jedis.setPassword(password);
        }
        if (database != 0) {
            jedis.setDatabase(database);
        }
        jedis.setPoolConfig(poolCofig());
        // 初始化连接pool
        jedis.afterPropertiesSet();

        return jedis;
    }

    public JedisPoolConfig poolCofig() {
        JedisPoolConfig poolCofig = new JedisPoolConfig();
        poolCofig.setMaxIdle(maxIdle);
        poolCofig.setMinIdle(minIdle);
        poolCofig.setMaxTotal(maxActive);
        return poolCofig;
    }
		
		
	
}
